package com.example.baseweather.ui.view.lyric.util;

import com.example.baseweather.ui.view.lyric.bean.LyricBean;
import com.example.baseweather.ui.view.lyric.bean.LyricContentBean;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LyricIUtil {


    public static LyricBean getLyricData(File lyricFile) {
        try (
                final FileReader fileReader = new FileReader(lyricFile);
                final BufferedReader bufferedReader = new BufferedReader(fileReader);
        ) {
            if (!lyricFile.getName().endsWith(".lrc")) {
                throw new FileNotFoundException("当前歌词未找到!");
            }
            String lyricTempString;
            final LinkedList<LyricContentBean> lyricContentBeans = new LinkedList<>();
            final LyricBean lyricBean = new LyricBean();
            final Pattern contentPattern =
                    Pattern.compile("\\[(\\d{2}):(\\d{2}).(\\d{2})\\](\\S+.*)");
            final Pattern infoPattern = Pattern.compile("\\[(\\w*):(\\S+)\\]");


            while ((lyricTempString = bufferedReader.readLine()) != null) {
                final Matcher matcher = contentPattern.matcher(lyricTempString);
                if (matcher.matches()) {
                    final LyricContentBean lyricContentBean = new LyricContentBean();

                    if (matcher.groupCount() >= 4) {
                        final Calendar instance = Calendar.getInstance();
                        final String group1 = matcher.group(1);
                        final String group2 = matcher.group(2);
                        final String group3 = matcher.group(3);
                        final String group4 = matcher.group(4);
                        instance.set(Calendar.MINUTE, Integer.parseInt(group1));
                        instance.set(Calendar.SECOND, Integer.parseInt(group2));
                        instance.set(Calendar.MILLISECOND,Integer.valueOf(group3)*10);

                        final long timeInMillis = instance.getTimeInMillis();
                        lyricContentBean.setPlayTimeDuration(timeInMillis);

                        final String timeString = new StringBuilder().append(group1)
                                .append(":").append(group2).append(":").append(group3).toString();
                        lyricContentBean.setPlayTime(timeString);
                        lyricContentBean.setLyricContent(group4);
                        lyricContentBeans.add(lyricContentBean);
                    }
                }
                final Matcher matcher1 = infoPattern.matcher(lyricTempString);
                if (matcher1.matches()) {
                    if (matcher.groupCount() > 2) {
                        //detail
                        final String group1 = matcher1.group(1);
                        //info
                        final String group2 = matcher1.group(2);
                        switch (group1) {
                            // [ti:理想三旬] [ar:陈鸿宇][al:众乐纪壹] [by:] [offset:0]
                            case "ti":
                                lyricBean.setSongName(group2);
                                break;
                            case "ar":
                                lyricBean.setArtistName(group2);
                                break;
                            case "al":
                                lyricBean.setCompanyName(group2);
                                break;
                            case "by":
                                lyricBean.setSongMaker(group2);
                                break;
                            case "offset":
                                lyricBean.setOffset(Integer.parseInt(group2));
                                break;
                        }
                    }
                }

            }
            lyricBean.setLyricContentList(lyricContentBeans);
            return lyricBean;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
